package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.service.impl.AdminOrderServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketOrderDetailVo;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

@WebServlet("/admin/order/*")
public class AdminOrderController extends HttpServlet {

    AdminOrderService adminOrderService= new AdminOrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");
        if("list".equals(op)){
            list(req,resp);
        }
        if("detail".equals(op)){
            detail(req,resp);
        }
        if("refund".equals(op)){
            refund(req,resp);
        }
        if("channel".equals(op)){
            channel(req,resp);
        }


    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");

        if("refund".equals(op)){
            refund(req,resp);
        }
        if("ship".equals(op)){
            ship(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
        if("reply".equals(op)){
            reply(req,resp);
        }

    }

    private void reply(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> replyHashMap = new HashMap<>();
        Map map = JacksonUtil.getObjectMapper().readValue(req.getReader().readLine(), replyHashMap.getClass());
        MarketComment marketComment = new MarketComment();
        marketComment.setId(( int)map.get("commentId"));
        marketComment.setContent((String) map.get("content"));
        int effectRows = adminOrderService.updateComment(marketComment);
        String resJson=null;
        if(effectRows==-1){
            resJson = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badReplyAgain());
        }
        else if(effectRows==1){
            resJson = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok());

        }
        else{
            resJson = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badReply());
        }
        resp.getWriter().write(resJson);


    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, Object> hashMapBo = new HashMap<>();


        hashMapBo= JacksonUtil.getObjectMapper().readValue(req.getReader().readLine(), hashMapBo.getClass());


        MarketOrder marketOrder=new MarketOrder();
        int id=(Integer) hashMapBo.get("orderId");


        marketOrder.setId(id);
        Integer[] banOrderNums = {101, 201,202, 301};
        ArrayList<Integer> banMarketOrder = new ArrayList<Integer>(Arrays.asList(banOrderNums));

        int effectRows=adminOrderService.delete(marketOrder,banMarketOrder);
        String resJson=null;
        if(effectRows==1){
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok());

        }
        else{
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badDelete());
        }
        resp.getWriter().println(resJson);
    }

    private void channel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,String> channel = (Map) req.getServletContext().getAttribute("channel");
        List<Map<String,String>> list=new ArrayList();
        for (String s : channel.keySet()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("code",s);
            map.put("name",channel.get(s));
            list.add(map);
        }
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(list)));
    }

    private void ship(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, Object> hashMapBo = new HashMap<>();


        hashMapBo= JacksonUtil.getObjectMapper().readValue(req.getReader().readLine(), hashMapBo.getClass());


        MarketOrder marketOrder=new MarketOrder();
        int id=(Integer) hashMapBo.get("orderId");

        String channel=(String) hashMapBo.get("shipChannel");

        String shipSn=(String) hashMapBo.get("shipSn");
        marketOrder.setId(id);

        marketOrder.setUpdateTime(LocalDateTime.now());
        marketOrder.setOrderStatus((short)401);
        marketOrder.setShipSn(shipSn);
        marketOrder.setShipChannel(channel);

        int effectRows=adminOrderService.update(marketOrder);
        String resJson=null;
        if(effectRows==1){
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok());

        }
        else{
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badShip());
        }
        resp.getWriter().println(resJson);
    }
    private void refund(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, Object> hashMapBo = new HashMap<>();


        hashMapBo= JacksonUtil.getObjectMapper().readValue(req.getReader().readLine(), hashMapBo.getClass());


        MarketOrder marketOrder=new MarketOrder();
        int id=(Integer) hashMapBo.get("orderId");
        marketOrder.setId(id);

        marketOrder.setUpdateTime(LocalDateTime.now());
        marketOrder.setOrderStatus((short)203);
        int effectRows=adminOrderService.update(marketOrder);
        String resJson=null;
        if(effectRows==1){
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok());

        }
        else{
            resJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badRefund());
        }
        resp.getWriter().println(resJson);

    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id=req.getParameter("id");
        MarketOrderDetailVo detail = adminOrderService.detail(id);
        String resJson=null;
        if(detail!=null){
            resJson = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(detail));
        }
        else{
            resJson = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badList());
        }
        resp.getWriter().println(resJson);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String order = req.getParameter("order");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String start =req.getParameter("start");
        String end=req.getParameter("end");
        List<MarketOrder> list = adminOrderService.list(page, limit,  sort,order, start, end);
        String respJson=null;
        if(list!=null){
            respJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.okList(list));

        }
        else{
            respJson=JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badList());
        }
        resp.getWriter().println(respJson);

    }
}


